﻿@model OrchardHUN.ModuleProfiles.ViewModels.ProfileListViewModel
           
@{
    Style.Require("OrchardHUN.ModuleProfiles.Style");
    Script.Require("jQuery").AtHead();
}

<script type="text/javascript">
    includeModuleInProfile = function(index) {
        if ($("#Current_Modules_" + index + "__Included").is(':checked')) $("#Current_Modules_" + index + "__Enabled").removeAttr("disabled");
        else $("#Current_Modules_" + index + "__Enabled").attr("disabled", true);
    }

    onMouseOverRow = function(index) {
        $("#moduleprofiles-row-" + index).addClass("moduleprofiles-selected-row");
    }

    onMouseOutRow = function(index) {
        $("#moduleprofiles-row-" + index).removeClass("moduleprofiles-selected-row");
    }

    $(document).ready(function () {
        $("tr:odd").addClass("moduleprofiles-odd-row");

        $("#moduleprofiles-view").click(function() {
            window.location = "@Url.Action("Index")" + "?profileName=" + $("#moduleprofiles-list option:selected").val();
        });

        $("#moduleprofiles-activate").click(function () {
            var name = $("#moduleprofiles-list option:selected").val();
            $.ajax({
                type: "POST",
                url: "@Url.Action("ActivateProfile")",
                data: {
                    __RequestVerificationToken: $(':input[name="__RequestVerificationToken"]').val(),
                    Name: name
                },
                success: function() {
                    window.location = "@Url.Action("Index")" + "?profileName=" + name;
                }
            });           
        });

        $("#moduleprofiles-inverse-activate").click(function () {
            var name = $("#moduleprofiles-list option:selected").val();
            $.ajax({
                type: "POST",
                url: "@Url.Action("InverseActivateProfile")",
                data: {
                    __RequestVerificationToken: $(':input[name="__RequestVerificationToken"]').val(),
                    Name: name
                },
                success: function() {
                    window.location = "@Url.Action("Index")" + "?profileName=" + name;
                }
            });           
        });

        $("#moduleprofiles-delete").click(function () {
            $.ajax({
                type: "DELETE",
                url: "@Url.Action("DeleteProfile")",
                data: {
                    __RequestVerificationToken: $(':input[name="__RequestVerificationToken"]').val(),
                    Name: $("#moduleprofiles-list option:selected").val()
                },
                success: function() {
                    window.location = "@Url.Action("Index")";
                }
            });           
        });

        $("#moduleprofiles-new").click(function () {
            var name = $("#moduleprofiles-name").val();
            $.ajax({
                type: "POST",
                url: "@Url.Action("CreateProfile")",
                data: {
                    __RequestVerificationToken: $(':input[name="__RequestVerificationToken"]').val(),
                    Name: name
                },
                success: function() {
                    window.location = "@Url.Action("Index")" + "?profileName=" + name;
                }
            });           
        });

        $("#moduleprofiles-save-configuration").click(function () {
            var name = $("#moduleprofiles-name").val();
            $.ajax({
                type: "POST",
                url: "@Url.Action("SaveConfiguration")",
                data: {
                    __RequestVerificationToken: $(':input[name="__RequestVerificationToken"]').val(),
                    Name: name
                },
                success: function() {
                    window.location = "@Url.Action("Index")" + "?profileName=" + name;
                }
            });           
        });
    });
</script>

<h1>@Html.TitleForPage(T("Module Profiles").ToString())</h1>

@using (Html.BeginFormAntiForgeryPost())
{
    @Html.Label("model", T("Here is a list of the available module profiles:").ToString())
    @Html.DropDownListFor(model => model.ProfileNames.Values, new SelectList(Model.ProfileNames.Values), new { id = "moduleprofiles-list" })

    <input type="button" id="moduleprofiles-view" class="button" value="@T("View")" />
    <input type="button" id="moduleprofiles-activate" class="button" value="@T("Activate")" />
    <input type="button" id="moduleprofiles-inverse-activate" class="button" value="@T("Activate Inverse")" />
    <input type="button" id="moduleprofiles-delete" class="button" value="@T("Delete")" />
}

<br />
<br />

@using (Html.BeginFormAntiForgeryPost())
{
    @Html.TextBox("Name", "", new { id = "moduleprofiles-name" })
    <input type="button" id="moduleprofiles-new" class="button" value="@T("Create New Profile")" />
    <input type="button" id="moduleprofiles-save-configuration" class="button" value="@T("Save Current Configuration")" />
}

<br />
<br />

@if (Model.Current != null)
{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#moduleprofiles-list").val("@Model.Current.Name").attr("selected", "selected");
        });
    </script>
    
    using (Html.BeginFormAntiForgeryPost(Url.Action("SaveProfile"), FormMethod.Post))
    {
    <h1>@T("Selected profile:\t")@Html.DisplayTextFor(m => Model.Current.Name) (@Model.Current.Modules.Count(m => m.Included))</h1>
        @Html.HiddenFor(m => Model.Current.Name)
    
    <button type="submit" class="primaryAction">@T("Save Profile")</button>
    
    <br />
    <br />
        
    <table id="moduleprofiles-table">
        <thead>
            <tr>
                <td class="moduleprofiles-centered-column">#</td>
                <td>@T("Included")</td>
                <td>@T("Enabled")</td>
                <td>@T("Current state")</td>
                <td>@T("Module name")</td>
            </tr>
        </thead>
        <tbody>
        @for (int i = 0; i < Model.Current.Modules.Count; i++)
        {
            var index = i + 1;
            <tr id="moduleprofiles-row-@index" onmouseover="onMouseOverRow(@index)" onmouseout="onMouseOutRow(@index)">
                <td class="moduleprofiles-centered-column">@index</td>
                <td>@Html.CheckBoxFor(m => Model.Current.Modules[i].Included, new { onClick = "includeModuleInProfile(\"" + @i + "\")" })</td>
                @if (Model.Current.Modules[i].Included)
                {
                    <td>@Html.CheckBoxFor(m => Model.Current.Modules[i].Enabled)</td>
                }
                else
                {
                    <td>@Html.CheckBoxFor(m => Model.Current.Modules[i].Enabled, new { disabled = true })</td>
                }
                @if (@Model.Current.Modules[i].State)
                {
                    <td>@T("Enabled")</td>
                }
                else
                {
                    <td>@T("Disabled")</td>
                }
                <td>
                    @Html.DisplayTextFor(m => Model.Current.Modules[i].Name)
                    @Html.HiddenFor(m => Model.Current.Modules[i].Name)
                </td>
            </tr>
        }
        </tbody>
    </table>
    
    <br />
    
    <button type="submit" class="primaryAction">@T("Save Profile")</button>
    }

}
else
{
    <h2>@T("No profile selected")</h2>
}